package threadpoolnio.server;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class javaTest01 {



    public static boolean login(Map<String, String> userLoginInfo) {
        //JDBC代码；
//        ResourceBundle bundle=ResourceBundle.getBundle("jdbc");
//        String driver=bundle.getString("driver");
//        String url=bundle.getString("url");
//        String user=bundle.getString("user");
//        String password=bundle.getString("password");
        Connection conn=null;
        Statement stmt=null;
        boolean loginSuccess = false;
        ResultSet rs = null;
        String loginName = userLoginInfo.get("loginName");
        String loginPwd = userLoginInfo.get("loginPwd");
        try {
            //1、注册驱动；
            Class.forName("com.mysql.jdbc.Driver");
            //2、获取连接；
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/qqsql", "root", "998921");
            //3、获取数据库操作对象；
            stmt = conn.createStatement();
            System.out.println(stmt);
            //4、执行SQl语句；
            String sql = "select * from t_qq where user='" + loginName + "' and pwd='" + loginPwd + "'";
            //5、处理结果集；
            rs = stmt.executeQuery(sql);
            if (rs.next()) {
                //
                loginSuccess = true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        //6、释放资源；
        finally {

            if (rs != null) {
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            //释放资源
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

        }
        return loginSuccess;

    }

    public static Map<String, String> initUI(String loginName, String loginPwd) {
        Scanner s = new Scanner(System.in);

        Map<String, String> userLoginInfo = new HashMap<>();
        userLoginInfo.put("loginName", loginName);
        userLoginInfo.put("loginPwd", loginPwd);
        return userLoginInfo;
    }

    public static void register(String str1, String str2) {

//
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        String loginName = str1;
        String loginPwd = str2;
        try {
            //1、注册驱动；
            Class.forName("com.mysql.jdbc.Driver");
            //2、获取连接；
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/qqsql","root", "998921");
            //3、获取数据库操作对象；
            stmt = conn.createStatement();
            System.out.println(stmt);
            //4、执行SQl语句；
            String sql="insert into t_qq(user,pwd)value('"+loginName+"','"+loginPwd+"')";
            int count =stmt.executeUpdate(sql);
            System.out.println(count==1 ? "注册成功":"注册失败");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //6、释放资源；
        finally {

            if (rs != null) {
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            //释放资源
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}

